package com.ctrl.riveradministrator.http.download;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BaseHandler;
import com.ctrl.riveradministrator.common.Constants;
import com.ctrl.riveradministrator.common.What;
import com.ctrl.riveradministrator.http.APIService;
import com.ctrl.riveradministrator.http.RetrofitUtils;
import com.ctrl.riveradministrator.mvp.view.activity.file_information.FileActivity;
import com.ctrl.riveradministrator.mvp.view.activity.file_information.FileInfoActivity;

import java.io.File;


public class DownloadService extends IntentService {

    private static final String TAG = "DownloadService";
    private String mDownloadFileName;
    private DownloadUtil downloadUtil;
    private FileInfoActivity.FileDownloadHandler fileDownloadHandler;
    private BaseHandler handler;
    protected APIService apiService;


    public DownloadService() {
        super("DownloadService");
        apiService = RetrofitUtils.getInstance().getApiService();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        downloadUtil = new DownloadUtil();
        fileDownloadHandler = new FileInfoActivity.FileDownloadHandler(getMainLooper());
        String downloadUrl = intent.getExtras().getString("download_url");
        mDownloadFileName = intent.getExtras().getString("download_file");

        Log.d(TAG, "download_url --" + downloadUrl);
        Log.d(TAG, "download_file --" + mDownloadFileName);

        final File file = new File(Constants.APP_ROOT_PATH + Constants.DOWNLOAD_DIR + mDownloadFileName);
        long range = 0;
        int progress = 0;

        if (file.exists()) {
            range = SPDownloadUtil.getInstance(this).get(downloadUrl, 0);
            progress = (int) (range * 100 / file.length());
            if (range == file.length()) {

                return;
            }
        }
        downloadUtil.downloadFile(downloadUrl, new DownloadListener() {
            @Override
            public void onProgress(int progress) {
                Log.d(TAG, "pro:" + progress);
                Message message = fileDownloadHandler.obtainMessage();
                message.what = What.FILE_DOWNLOADING;
                message.arg1 = progress;
                fileDownloadHandler.sendMessage(message);
            }

            @Override
            public void onCompleted(File file) {
                Message message = fileDownloadHandler.obtainMessage();
                message.what = What.FILE_COMPLETED;
                message.obj = file;
                fileDownloadHandler.sendMessage(message);
            }

            @Override
            public void onError(String msg) {
                fileDownloadHandler.sendEmptyMessage(What.FILE_ERROR);
            }

        });
        /*downloadUtil.downloadFile(this, apiService,
                range, downloadUrl, mDownloadFileName, new DownloadListener() {
                    @Override
                    public void onProgress(int progress) {
                        Log.d(TAG, "pro:" + progress);
                        Message message = fileDownloadHandler.obtainMessage();
                        message.what = What.FILE_DOWNLOADING;
                        message.arg1 = progress;
                        //fileDownloadHandler.sendMessage(message);
                    }

                    @Override
                    public void onCompleted(File file) {
                        Message message = fileDownloadHandler.obtainMessage();
                        message.what = What.FILE_COMPLETED;
                        message.obj = file;
                        //fileDownloadHandler.sendMessage(message);
                    }

                    @Override
                    public void onError(String msg) {
                       // fileDownloadHandler.sendEmptyMessage(What.FILE_ERROR);
                    }
                });*/
    }
}
